<?php

class PageListEvents extends Page
{
	public function getHTML()
	{
		$html = $this->getHeaderHTML();

		$submitScript = new SubmitFormScript();
		$html .= $submitScript->getHTML();

		$script = new EditDeleteFormScript();
		$html .= $script->getHTML();

		$html .= $this->getBodyTopHTML();
		$html .= $this->getHiddenVariables();

		$View = new Hidden();
		$View->setName("View");
		$View->setValue('List');
		$html .= $View->gethtml();

		$View = new Hidden();
		$View->setName("CallingPage");
		$View->setValue('ListEvents');
		$html .= $View->gethtml();



		$datatable = new DataTable();
		$datatable->setParentPage($this);
		$datatable->setName("Events");
		$datatable->setValue("Events");
		$datatable->setEditable(true);
		$datatable->setDeletable(true);
/*		if(isset($_POST['ShowDeleted']))
			$datatable->showDeleted($_POST['ShowDeleted']);
		if(isset($_POST['ShowBefore']))
			$datatable->showBefore($_POST['ShowBefore']);
		if(isset($_POST['ShowAfter']))
			$datatable->showAfter($_POST['ShowAfter']);
		elseif((!isset($_POST['ShowBefore']))&&(!isset($_POST['ShowAfter'])))
			$datatable->showAfter(true);
		if(isset($_POST['ShowLocations']))
			$datatable->showLocations(true);
		if(isset($_POST['ShowEventInvolvees']))
			$datatable->showEventInvolvees(true);
		if(isset($_POST['ShowEventTypes']))
			$datatable->showEventTypes(true);*/
		$datatable->setDBTableName("Events");
		$datatable->setDatabase($this->Database);
		$theRenderer = new DataTableRendererEdit($datatable);
		$theRenderer->setAction = "ListEvents";
		$datatable->setRenderer($theRenderer);
		$columns = array();
		$columns[] = new DataTableColumn("Start","Date","DateOnly");
		$columns[] = new DataTableColumn("Start","Start","TimeOnly");
		$columns[] = new DataTableColumn("End","End","TimeOnly");
		$columns[] = new DataTableColumn("Title","Title","");
		$columns[] = new DataTableColumn("Description","Description","");
		$columns[] = new DataTableColumn("Comment","Comment","");
		$columns[] = new DataTableColumn("Responsible","Responsible","");
		$columns[] = new DataTableColumn("EventTypeID","Event Type","SingleEnumeration");
		$columns[] = new DataTableColumn("EventInvolvee","Event Involves ...","MultiEnumeration");
		$columns[] = new DataTableColumn("Location","Location","MultiEnumeration");
		$datatable->setColumns($columns);
		
		$InvolveesInnerJoin = ((isset($_POST['frShowInvolvees']))&&(isset($_POST['frShowInvolveesMultiEnum'])))?"INNER JOIN EventInvolvees AS ei ON ev.EventID = ei.EventID":"";
		$InvolveesTable = ((isset($_POST['frShowInvolvees']))&&(isset($_POST['frShowInvolveesMultiEnum'])))?",ei.EventInvolveeNameID as EventInvolveeNameID ":"";

		$LocationsInnerJoin = ((isset($_POST['frShowLocations']))&&(isset($_POST['frShowLocationsMultiEnum'])))?"INNER JOIN Locations AS loc ON ev.EventID = loc.EventID":"";
		$LocationsTable = ((isset($_POST['frShowLocations']))&&(isset($_POST['frShowLocationsMultiEnum'])))?",loc.LocationNameID as LocationNameID ":"";

		$datatable->setSelect("SELECT ev.EventID AS ColumnID, ev.EventID AS EventID, ev.CreatedWhen AS CreatedWhen,ev.CreatedBy AS CreatedBy,ev.DeletedWhen AS DeletedWhen,ev.DeletedBy AS DeletedBy,ev.Start AS Start, ev.End AS End, ev.Title AS Title, ev.Description AS Description, ev.Comment AS Comment, ev.Responsible AS Responsible, ev.EventTypeID AS EventTypeID,ev.EventInvolvee AS EventInvolvee,ev.Location AS Location ".$InvolveesTable." ".$LocationsTable." FROM Events AS ev ".$InvolveesInnerJoin." ".$LocationsInnerJoin);
		$datatable->setOrderBy("Start");
		
// Add filter requests

// Add filter requests

		// only display non-deleted unless specifically requested
/*		$aFilterRequest = new FilterRequest();
		$aFilterRequest->setName("frShowDeleted");
		$aFilterRequest->setFilterType("None");
		$aFilterRequest->setIsORCondition(true);
		$aFilterRequest->setConditionType("SQL");
		$aFilterRequest->setSQLCondition("DeletedBy <> ''");
		$aFilterRequest->setEnabled(true);
		$datatable->addFilterRequest($aFilterRequest);
*/		
// Time

		$dtNow = new DateTime("now");
		$dtNow->modify("-1 day");
		$valueNow = $dtNow->format("Y-m-d");
		

		// show after
		$aFilterRequest = new FilterRequest();
		$aFilterRequest->setName("frShowAfter");
		$aFilterRequest->setFilterType("Date");
		$aFilterRequest->setText("Show after");
		$aFilterRequest->setField("Start");
		if(isset($_POST['frShowAfterDate']))
			$aFilterRequest->setValue($_POST['frShowAfterDate']);
		else
			$aFilterRequest->setValue($valueNow);
		$aFilterRequest->setIsORCondition(true);
		$isEnabled = false;
		if(isset($_POST['frShowAfter']))
			$isEnabled = ($_POST['frShowAfter']=="on");
		// if none of the possible filters is enabled then Show after should be
		if	(	
			(!isset($_POST['frShowDeleted']))
			&&
			(!isset($_POST['frShowAfter']))
			&&
			(!isset($_POST['frShowCreatedAfter']))
			&&
			(!isset($_POST['frShowDeletedAfter']))
			&&
			(!isset($_POST['frShowInvolvees']))
			&&
			(!isset($_POST['frShowLocations']))
			)
				$isEnabled = true;
		$aFilterRequest->setEnabled($isEnabled);
		$datatable->addFilterRequest($aFilterRequest);
		


//********************************************************************************
		// show involvees
		$aFilterRequest = new FilterRequest();
		$aFilterRequest->setName("frShowInvolvees");
		$aFilterRequest->setFilterType("MultiEnum");
		$aFilterRequest->setText("Show Involvees");
		$aFilterRequest->setField("EventInvolvee");
		$enum = new Enumeration($this->Database->Recopy(),'EventInvolveeNames');
		$aFilterRequest->setEnum($enum->getArray());
		if((isset($_POST['frShowInvolvees']))&&(isset($_POST['frShowInvolveesMultiEnum'])))
			$aFilterRequest->setValue($_POST['frShowInvolvees']);
		$aFilterRequest->setIsORCondition(false);
		$isEnabled = false;
		if((isset($_POST['frShowInvolvees']))&&(isset($_POST['frShowInvolveesMultiEnum'])))
		{
			$isEnabled = ($_POST['frShowInvolvees']=="on");
//			echo "isEnabled = ".$isEnabled."<br>";
		}
		$aFilterRequest->setEnabled($isEnabled);
//		$aFilterRequest->setEnabled($isEnabled);
		$datatable->addFilterRequest($aFilterRequest);

		
//********************************************************************************
		// show locations
		$aFilterRequest = new FilterRequest();
		$aFilterRequest->setName("frShowLocations");
		$aFilterRequest->setFilterType("MultiEnum");
		$aFilterRequest->setText("Show Locations");
		$aFilterRequest->setField("Location");
		$enum = new Enumeration($this->Database->Recopy(),'LocationNames');
		$aFilterRequest->setEnum($enum->getArray());
		if((isset($_POST['frShowLocations']))&&(isset($_POST['frShowLocationsMultiEnum'])))
			$aFilterRequest->setValue($_POST['frShowLocations']);
		$aFilterRequest->setIsORCondition(false);
		$isEnabled = false;
		if((isset($_POST['frShowLocations']))&&(isset($_POST['frShowLocationsMultiEnum'])))
		{
			$isEnabled = ($_POST['frShowLocations']=="on");
//			echo "isEnabled = ".$isEnabled."<br>";
		}
		$aFilterRequest->setEnabled($isEnabled);
//		$aFilterRequest->setEnabled($isEnabled);
		$datatable->addFilterRequest($aFilterRequest);

//********************************************************************************
		// show created after
		$aFilterRequest = new FilterRequest();
		$aFilterRequest->setName("frShowCreatedAfter");
		$aFilterRequest->setFilterType("Date");
		$aFilterRequest->setText("Show created since");
		$aFilterRequest->setField("CreatedWhen");
//		echo $_POST['frShowCreatedAfterDate']."<br>";
		if(isset($_POST['frShowCreatedAfterDate']))
			$aFilterRequest->setValue($_POST['frShowCreatedAfterDate']);
		else
		{
			$dtNow = new DateTime("now");
			$dtNow->modify("-1 weeks");
			$valueNow = $dtNow->format("Y-m-d");
//			echo $valueNow."<br>";
			$aFilterRequest->setValue($valueNow);
		}
		$aFilterRequest->setIsORCondition(true);
		$isEnabled = false;
		if(isset($_POST['frShowCreatedAfter']))
			$isEnabled = ($_POST['frShowCreatedAfter']=="on");
		$aFilterRequest->setEnabled($isEnabled);
//		echo $aFilterRequest->getFilterCondition()."<br>";
		$datatable->addFilterRequest($aFilterRequest);

//********************************************************************************
		// show deleted after
		$aFilterRequest = new FilterRequest();
		$aFilterRequest->setName("frShowDeletedAfter");
		$aFilterRequest->setFilterType("Date");
		$aFilterRequest->setText("Show deleted since");
		$aFilterRequest->setField("DeletedWhen");
//		echo $_POST['frShowCreatedAfterDate']."<br>";
		if(isset($_POST['frShowDeletedAfterDate']))
			$aFilterRequest->setValue($_POST['frShowDeletedAfterDate']);
		else
		{
			$dtNow = new DateTime("now");
			$dtNow->modify("-1 weeks");
			$valueNow = $dtNow->format("Y-m-d");
//			echo $valueNow."<br>";
			$aFilterRequest->setValue($valueNow);
		}
		$aFilterRequest->setIsORCondition(true);
		$isEnabled = false;
		if(isset($_POST['frShowDeletedAfter']))
			$isEnabled = ($_POST['frShowDeletedAfter']=="on");
		$aFilterRequest->setEnabled($isEnabled);
//		echo $aFilterRequest->getFilterCondition()."<br>";
		$datatable->addFilterRequest($aFilterRequest);
		
		
// Show Deleted
 		if(!$isEnabled)
 		{
 		
	//		echo "frShowDeleted = ".$_POST['frShowDeleted']."<br>";
			$aFilterRequest = new FilterRequest();
			$aFilterRequest->setName("frShowDeleted");
			$aFilterRequest->setFilterType("none");
			$aFilterRequest->setConditionType("SQL");
			$aFilterRequest->setIsORCondition(false);
			$aFilterRequest->setEnabled(true);
			$sqlCondition = "DeletedBy = ''";
			$aFilterRequest->setSQLCondition($sqlCondition);
			$datatable->addFilterRequest($aFilterRequest);
 		}
		
		

		$html .= $datatable->getHTML();
		$html .= "<br>\n";

		$html .= $this->getBodyBottomHTML();
		return $html;
	}
}
?>